<?php


/**
 * Ope Admin Class
 *
 */
class Login extends K9_Deploy_Controller {
	
	public $user;
	
	/**
	 * Constructor
	 */
	function __construct()
	{
		parent::__construct();
		
		$this->load->helper('url');
		$this->load->helper('cookie');
		$this->load->helper('form');
		$this->load->helper('ajax');
		$this->settings->load('backend');
		
		$this->load->library('session');
		$this->load->library('deploy/deploy_lib');
	}
	
	/**
	 * 默认入口
	 *
	 * @access  public
	 * @return  void
	 */
	public function index()
	{
        $data['language'] = $this->language;

		if ($this->session->userdata('uid'))
		{
            		redirect($this->language.'/welcome/view');
		}
		else
		{
			$this->load->view($this->language.'/login',$data);
		}
	}
	
	
	/**
	 * Login page of OPE
	 *
	 * Login with ope's nickname and password
	 *
	 * @return	Login page
	 */
	public function do_login()
	{
// 		$this->output->enable_profiler(TRUE);
	
		$uname = $this->input->post('uname');
		$pwd = $this->input->post('pwd');

        $data['language'] = $this->language;

        if(empty($uname) || empty($pwd))
		{
			if(empty($uname))
				$this->session->set_flashdata('error', 'username is empty');
			
			if(empty($pwd))
				$this->session->set_flashdata('error', 'password is empty');
			
			$this->load->view($this->language.'/login',$data);
			return;
		}

		$this->user = $this->deploy_lib->get_account($uname, $pwd);
		
		if(empty($this->user))
		{
			$this->session->set_flashdata('error', 'account is incorrect!');
			$this->load->view($this->language.'/login',$data);
			return;
		}
	
		$this->session->set_userdata('uid', $this->user->uid);
		
		redirect($this->language.'/welcome/view/');
		return;
	}
	
	/**
	 * Logout
	 *
	 * remove uid and token cookies
	 *
	 * @return	login page
	 */
	public function do_logout()
	{
		$this->session->sess_destroy();

        $data['language'] = $this->language;

        redirect($this->language.'/login',$data);
		return;
	}
}
